package com.ztf.room.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztf.room.bean.RoomAdmin;
import com.ztf.room.bean.RoomApplyResults;
import com.ztf.room.bean.RoomApplys;
import com.ztf.room.bean.StudyRooms;
import com.ztf.room.dao.StudyRoomDao;
import com.ztf.room.service.StudyRoomService;
import com.ztf.room.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudyRoomServiceImp implements StudyRoomService {

    @Autowired
    private StudyRoomDao studyRoomDao;

    @Override
    public PageInfo<StudyRooms> getRoomList(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<StudyRooms> list = studyRoomDao.selectRoomList();
        return PageInfo.of(list);
    }

    @Transactional
    @Override
    public boolean saveApplyRoom(RoomApplys roomApplys) {

        // 查看当前申请区间是否,还在自习室里,如果存在,就不申请,
//        int count  = studyRoomDao.checkRoomApply(roomApplys);

        // 设置提交时间
        roomApplys.setApplyDate(TimeUtils.nowTime());
        // 设置为正在等待
        roomApplys.setStatus("3");
        // 添加提交记录
        int i = studyRoomDao.addApplyRoom(roomApplys);
        return i==1;
    }

    @Override
    public PageInfo<RoomApplys> getApplyResultsByStu(int studentId, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<RoomApplys> list = studyRoomDao.getApplyResultsByStu(studentId);
        return PageInfo.of(list);
    }

    @Override
    public PageInfo<RoomApplys> getApplyResults(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<RoomApplys> list =  studyRoomDao.getApplyResults();
        return PageInfo.of(list);
    }

    @Transactional
    @Override
    public boolean handleAppy(RoomApplyResults results, RoomAdmin roomAdmin) {
        // 修改申请表记录
        studyRoomDao.updateRoomApply(results);
        // 生成自习室申请处理记录
        results.setManagerName(roomAdmin.getAdminName());
        results.setManagerContact(roomAdmin.getAdminPhone());
        results.setManagerId(roomAdmin.getId());
        results.setApproveOrnot("1");
        studyRoomDao.addApplyRoomResult(results);
        return true;
    }
}
